package com.mxdx.edu.util;

import java.math.BigDecimal;

import javax.servlet.http.HttpServletRequest;

import com.mxdx.edu.common.log.AppLogger;
import com.mxdx.edu.constant.StaticConstants;

/**
 * 获取request中参数工具类
 * 
 * @author wangyuan
 * @date 2016-11-24
 *
 */
public class ServletUtil {

    private static AppLogger logger = new AppLogger(ServletUtil.class);

    private ServletUtil() {}

    /**
     * 获取request Parameter范围里面的值
     * 
     * @param request
     * @param params
     * @param defaultValue
     * @return
     */
    public static String getParameterStr(HttpServletRequest request, String params,
            String defaultValue) {
        String tmpStr = request.getParameter(params);
        return tmpStr == null ? defaultValue : tmpStr.trim();

    }

    /**
     * 从request中获取参数，并转换为int
     * 
     * @param request
     * @param params
     * @param defaultValue
     * @return
     */
    public static int getInt(HttpServletRequest request, String params, int defaultValue) {
        String tmpStr = request.getParameter(params);
        if (StringUtils.isBlank(tmpStr)) return defaultValue;
        int tmpInt = defaultValue;
        try {
            tmpInt = Integer.parseInt(tmpStr);
        } catch (NumberFormatException e) {
            logger.doError("getInt Exception", e);
        }
        return tmpInt;
    }

    /**
     * 从request中获取参数，并转换为float
     * 
     * @param request
     * @param params
     * @param defaultValue
     * @return
     */
    public static float getFloat(HttpServletRequest request, String params, float defaultValue) {
        String tmpStr = request.getParameter(params);
        if (StringUtils.isBlank(tmpStr)) return defaultValue;
        float tmpInt = defaultValue;
        try {
            tmpInt = Float.parseFloat(tmpStr);
        } catch (NumberFormatException e) {
            logger.doError("getFloat Exception", e);
        }
        return tmpInt;
    }

    /**
     * 从http head 获取int值
     * 
     * @param request
     * @param params
     * @param defaultValue
     * @return
     */
    public static int getIntFromHead(HttpServletRequest request, String params, int defaultValue) {
        String tmpStr = request.getHeader(params);
        if (StringUtils.isBlank(tmpStr)) return defaultValue;
        int tmpInt = defaultValue;
        try {
            tmpInt = Integer.parseInt(tmpStr);
        } catch (NumberFormatException e) {
            logger.doError("getIntFromHead Exception", e);
        }
        return tmpInt;
    }

    /**
     * 从http head 获取long值
     * 
     * @param request
     * @param params
     * @param defaultValue
     * @return
     */
    public static long getLongFromHead(HttpServletRequest request, String params, long defaultValue) {
        String tmpStr = request.getHeader(params);
        if (StringUtils.isBlank(tmpStr)) return defaultValue;
        long tmp = defaultValue;
        try {
            tmp = Long.parseLong(tmpStr);
        } catch (NumberFormatException e) {
            logger.doError("getLongFromHead Exception", e);
        }
        return tmp;
    }

    /**
     * 从http head 获取String值
     * 
     * @param request
     * @param params
     * @param defaultValue
     * @return
     */
    public static String getStrFromHead(HttpServletRequest request, String params,
            String defaultValue) {
        String tmpStr = request.getHeader(params);
        return tmpStr == null ? defaultValue : tmpStr;
    }

    /**
     * 从request中获取参数，并转换为BigDecimal
     * 
     * @param request
     * @param params
     * @param defaultValue
     * @return
     */
    public static BigDecimal getDecimal(HttpServletRequest request, String params,
            BigDecimal defaultValue) {
        String tmpStr = getParameterStr(request, params, "");
        BigDecimal value = defaultValue;
        if ("".equals(tmpStr)) return defaultValue;
        try {
            Double tmp = Double.parseDouble(tmpStr);
            value = BigDecimal.valueOf(tmp);
        } catch (NumberFormatException e) {
            logger.doError("parseInt Exception", e);
        }
        return value;
    }

    /**
     * 从request中获取参数，并转换为long
     * 
     * @param request
     * @param param
     * @param defaultValue
     * @return
     */
    public static long getLong(HttpServletRequest request, String param, long defaultValue) {
        String tmpStr = request.getParameter(param);
        if (StringUtils.isBlank(tmpStr)) return defaultValue;
        long tmp = defaultValue;
        try {
            tmp = Long.parseLong(tmpStr);
        } catch (NumberFormatException e) {
            logger.doError("getLong Exception", e);
        }
        return tmp;
    }

    /**
     * 从request中获取参数，并转换为short
     * 
     * @param request
     * @param param
     * @param defaultValue
     * @return
     */
    public static short getShort(HttpServletRequest request, String param, short defaultValue) {
        String tmpStr = request.getParameter(param);
        if (StringUtils.isBlank(tmpStr)) return defaultValue;
        short tmp = defaultValue;
        try {
            tmp = Short.parseShort(tmpStr);
        } catch (NumberFormatException e) {
            logger.doError("getShort Exception", e);
        }
        return tmp;
    }

    /**
     * 获取客户端IP
     * 
     * @param request
     * @return
     */
    public static String getClientIp(HttpServletRequest request) {
        String ip = request.getHeader("X-Forwarded-For");
        if (ip == null || ip.length() == 0 || StaticConstants.UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || StaticConstants.UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || StaticConstants.UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }

}
